package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ChargeCarInfoMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.pojo.car.*;
import com.zhentao.service.*;
import com.zhentao.userLocal.UserThreadLocalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author mp
 * @since 2024-12-18
 */
@Service
public class ChargeCarInfoServiceImpl extends ServiceImpl<ChargeCarInfoMapper, ChargeCarInfo> implements IChargeCarInfoService {

    @Resource
    private ICarBrandService carBrandService;

    @Resource
    private ICarFuelTypeService carFuelTypeService;

    @Resource
    private ICarDisplacementService carDisplacementService;

    @Resource
    private ICarBatteryCapacityService carBatteryCapacityService;

    @Resource
    private ICarLicensePlateService carLicensePlateService;

    @Override
    public ResponseResult select() {

        //遍历集合
        List<ChargeCarInfo> list = this.list();
        for (ChargeCarInfo chargeCarInfo : list) {
            //获取车辆品牌
            CarBrand carBrand = carBrandService.getOne(Wrappers.<CarBrand>lambdaQuery().eq(CarBrand::getId, chargeCarInfo.getCarBrandId()));
            chargeCarInfo.setBrand(carBrand.getBrand());
            //获取燃料类型
            CarFuelType fuelType = carFuelTypeService.getOne(Wrappers.<CarFuelType>lambdaQuery().eq(CarFuelType::getId, chargeCarInfo.getFuelTypeId()));
            chargeCarInfo.setFuelType(fuelType.getFuelType());
            //获取车辆排量
            CarDisplacement displacement = carDisplacementService.getOne(Wrappers.<CarDisplacement>lambdaQuery().eq(CarDisplacement::getId, chargeCarInfo.getDisplacementId()));
            chargeCarInfo.setDisplacement(displacement.getDisplacement());
            //获取电池容量
            CarBatteryCapacity batteryCapacity = carBatteryCapacityService.getOne(Wrappers.<CarBatteryCapacity>lambdaQuery().eq(CarBatteryCapacity::getId, chargeCarInfo.getBatteryCapacityId()));
            chargeCarInfo.setBattery(batteryCapacity.getBattery());
        }
        return ResponseResult.okResult(list);

    }

    @Override
    public ResponseResult selectOneCar(Integer id) {
        ChargeCarInfo chargeCarInfo = this.getById(id);
        //获取车辆品牌
        CarBrand carBrand = carBrandService.getOne(Wrappers.<CarBrand>lambdaQuery().eq(CarBrand::getId, chargeCarInfo.getCarBrandId()));
        chargeCarInfo.setBrand(carBrand.getBrand());
        //获取燃料类型
        CarFuelType fuelType = carFuelTypeService.getOne(Wrappers.<CarFuelType>lambdaQuery().eq(CarFuelType::getId, chargeCarInfo.getFuelTypeId()));
        chargeCarInfo.setFuelType(fuelType.getFuelType());
        //获取车辆排量
        CarDisplacement displacement = carDisplacementService.getOne(Wrappers.<CarDisplacement>lambdaQuery().eq(CarDisplacement::getId, chargeCarInfo.getDisplacementId()));
        chargeCarInfo.setDisplacement(displacement.getDisplacement());
        //获取电池容量
        CarBatteryCapacity batteryCapacity = carBatteryCapacityService.getOne(Wrappers.<CarBatteryCapacity>lambdaQuery().eq(CarBatteryCapacity::getId, chargeCarInfo.getBatteryCapacityId()));
        chargeCarInfo.setBattery(batteryCapacity.getBattery());
        return ResponseResult.okResult(chargeCarInfo);
    }

    @Override
    public ResponseResult addCar(ChargeCarInfo chargeCarInfo) {
        System.out.println(chargeCarInfo);
        //首先获取我们的登录人id
        Long user = UserThreadLocalUtil.getUser();
        int userId = user.intValue();
        System.out.println("登录人id"+userId);
        chargeCarInfo.setUserId(userId);
        // 拼接车牌号
        // 查询车牌号的前缀，也就是省的简称
        CarLicensePlate carLicensePlate = carLicensePlateService.getOne(Wrappers.<CarLicensePlate>lambdaQuery().eq(CarLicensePlate::getId, chargeCarInfo.getLicensePlateId()));
        chargeCarInfo.setCarNo(carLicensePlate.getCapital()+"."+chargeCarInfo.getCarNo());
        System.out.println("添加的车牌号为：---"+chargeCarInfo.getCarNo());
        //设置一个默认的图片
        chargeCarInfo.setAvatarImg("http://121.41.164.80:9090/api/v1/download-shared-object/aHR0cDovLzEyNy4wLjAuMToxOTAwMC9jZHovU25pcGFzdGVfMjAyNC0xMi0yNV8xMC0yNC0yNy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1JTlpOVEpURVpJOEE4RFc1QlRBUCUyRjIwMjQxMjI1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MTIyNVQwMjI2MTdaJlgtQW16LUV4cGlyZXM9NDMyMDAmWC1BbXotU2VjdXJpdHktVG9rZW49ZXlKaGJHY2lPaUpJVXpVeE1pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmhZMk5sYzNOTFpYa2lPaUpKVGxwT1ZFcFVSVnBKT0VFNFJGYzFRbFJCVUNJc0ltVjRjQ0k2TVRjek5URXpOamN4TUN3aWNHRnlaVzUwSWpvaWJXbHVhVzloWkcxcGJpSjkuZmpKVTNEeUkxRDYxX2xTMkhkMmhnbWRrRnREWWhTLUl5Ymh1S0ttQktpd2VuVXNNTlFyVXJzQVh1N3hZLVB2V3RqVWhSN2dhV004ZGVKc1haUG42WncmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JnZlcnNpb25JZD1udWxsJlgtQW16LVNpZ25hdHVyZT1jOWU0MjU4MDQzZTE5YjY0MGJmYjY2OWRiN2VmNDYzMDZjODNiMDc5OGZmMTJiMDA2NTM2MzE1M2RkNDUwODIy");
        //创建人
        chargeCarInfo.setCreateBy(userId);
        //创建时间
        chargeCarInfo.setCreateTime(LocalDateTime.now());
        //添加
        boolean save = this.save(chargeCarInfo);
        if (!save) {
            return ResponseResult.errorResult(500, "添加失败");
        }
        return ResponseResult.okResult("添加成功");
    }
}
